package handler

import (
	"fmt"
	"io"
	"iptv_server/v3/v2_src/handler/middleware"
	"iptv_server/v3/v2_src/utils"
	"net/http"
)

func initDebug(mux *http.ServeMux, middles ...middleware.MiddleWare) {
	h := middleware.ApplyMiddles(&debug{true}, middles...)
	mux.Handle("/api/setdebug", h)

	h = middleware.ApplyMiddles(&debug{false}, middles...)
	mux.Handle("/api/nodebug", h)
}

type debug struct {
	enable bool
}

// "/api/live/"
// "0:/1:api/2:live/3:/4:"
func (d *debug) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	fmt.Printf("url for debug r.URL.Path: %v\n", r.URL.Path)
	if d.enable {
		utils.Logger.SetLoglevel(utils.LevelDEBUG)
		io.WriteString(w, "开启DEBUG\r\n")
	} else {
		utils.Logger.SetLoglevel(utils.LevelINFO)
		io.WriteString(w, "关闭DEBUG\r\n")
	}
	w.WriteHeader(http.StatusOK)
}

// curl http://localhost:9966/api/setdebug
// curl http://localhost:9966/api/nodebug
//
